package org.jeecg.controller;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.domain.Goods;
import org.jeecg.domain.User;
import org.jeecg.result.ResultTool;
import org.jeecg.service.CollectService;
import org.jeecg.service.GoodsService;
import org.jeecg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;


    @RequestMapping(value = "/getCollects",method = RequestMethod.GET)
    public ResultTool getCollects(@RequestParam("username") String username){

        if (username == null){
            return new ResultTool(400,"获取失败");
        }
        User user = userService.queryByUsername(username);
        if (user == null){
            return new ResultTool(400,"获取失败");
        }
        List<Goods> goodsByUsername = collectService.getGoodsByUsername(username);
        return new ResultTool(goodsByUsername,200);
    }


    @RequestMapping(value = "addCollect",method = RequestMethod.POST)
    public ResultTool addCollect(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String goodId = (String) jsonObject.get("goodId");
        User user = userService.queryByUsername(username);
        if (user == null){
            return new ResultTool(400,"收藏失败");
        }
        Goods goods = goodsService.queryGoods(goodId);
        if (goods == null){
            return new ResultTool(400,"收藏失败");
        }
        Boolean result = collectService.insertCollect(username, goodId);
        if (result){
            return new ResultTool(200,"收藏成功");
        }
        return new ResultTool(400,"收藏失败");
    }


    @RequestMapping(value = "/cancelCollect",method = RequestMethod.POST)
    public ResultTool cancelCollect(@RequestBody JSONObject jsonObject){

        String username = (String) jsonObject.get("username");
        String goodId = (String) jsonObject.get("goodId");
        User user = userService.queryByUsername(username);
        if (user == null){
            return new ResultTool(400,"取消收藏失败");
        }
        Goods goods = goodsService.queryGoods(goodId);
        if (goods == null){
            return new ResultTool(400,"取消收藏失败");
        }
        Boolean result = collectService.cancelCollect(username, goodId);
        if (result){
            return new ResultTool(200,"取消收藏成功");
        }
        return new ResultTool(400,"取消收藏失败");
    }

    @RequestMapping(value = "/queryCollectByUserAndGoodId")
    public ResultTool queryCollectByUserAndGoodId(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String goodId = (String) jsonObject.get("goodId");
        if (username == null || goodId == null){
            return new ResultTool(400,"获取失败");
        }
        Boolean result;
        try {
            result = collectService.queryCollect(username, goodId);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultTool(400,"查询失败");
        }
        if (result){
            return new ResultTool(true,200);
        }
        return new ResultTool(false,200);
    }
























}
